package com.xuhuan.juc.assist;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @Author huan.xu
 * @Date 2021/12/29 16:46
 * @Description 有6个线程执行逻辑，主线程需要等待6个线程全部执行完成才执行第7个线程的逻辑。
 */
@Slf4j
public class CyclicBarrierDemo {
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(6, () -> {
            log.info("开始执行第7个线程");
        });

        for (int i = 0; i < 6; i++) {
            int threadNum = i + 1;
            new Thread(() -> {
                log.info("线程【{}】执行完毕", threadNum);
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}
